package com.xjgc.eip.service;

import com.alibaba.fastjson.JSON;
import com.xjgc.common.config.DbConn;
import com.xjgc.eip.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * http://localhost:8081/EIPProxyServer/Eip/XJGC/2CGetService/Hjwsd
 */
@Service
@Path("/XJGC/2CGetService")
public class ZxjEip2CGetService {

	@Autowired
	DbConn db;

	@GET
	@Path("/Chjwsd")
	@Produces(MediaType.APPLICATION_JSON)
	public List<ZxjEip2CHjwsd> getZxjEipChjwsd(@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime) {
		System.out.println("******************Chjwsd*******************"+startTime+"      "+endTime);
//		String startTime="2019-08-13 08:00:00";
//		String endTime ="2019-08-13 08:25:00";
		try {
			
			ArrayList<ZxjEip2CHjwsd> zxjEipChjwsdList = new ArrayList<ZxjEip2CHjwsd>(); 
			
			PreparedStatement pst = null;
			ResultSet rs = null;			
			String sql = "select * from zxj_eip_2chjwsd where collecttimw Between to_timestamp('"+startTime+"','yyyy-mm-dd hh24:mi:ss') And to_timestamp('"+
					endTime+"','yyyy-mm-dd hh24:mi:ss')";			
			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				ZxjEip2CHjwsd zxjEipVbeln = new ZxjEip2CHjwsd(rs.getString("supplier"), rs.getString("collectTimw"), rs.getString("yclkf"), rs.getString("yclkf_wd"), 
						rs.getString("yclkf_sd"), rs.getString("smt"), rs.getString("smt_wd"), rs.getString("smt_sd"), rs.getString("bfh"), 
						rs.getString("bfh_wd"), rs.getString("bfh_sd"), rs.getString("bkjc"),rs.getString("bkjc_wd"), rs.getString("bkjc_sd"), 
						rs.getString("bkkf"), rs.getString("bkkf_wd"), rs.getString("bkkf_sd"), rs.getString("zzsc"), rs.getString("zzsc_wd"), rs.getString("zzsc_sd")
						, rs.getString("factory"), rs.getString("standbyField1"), rs.getString("standbyField2"), rs.getString("standbyField3"), rs.getString("standbyField4")
						, rs.getString("standbyField5"));
				zxjEipChjwsdList.add(zxjEipVbeln);
			}
			rs.close();
			pst.close();
			db.closeCn();
			System.out.println("*************************   Chjwsd  ==   "+ JSON.toJSONString(zxjEipChjwsdList));
			return zxjEipChjwsdList;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}	

	@GET
	@Path("/Chbzs")
	@Produces(MediaType.APPLICATION_JSON)	
	public ZxjEip2Chbssjc getZxjEipChbzs(@QueryParam("smtBkbh") String smt_bkbh, @QueryParam("point") String point) {
		System.out.println("***************Chbzs**************  "+smt_bkbh+"   "+point);
//		String smt_bkbh="1K819333100006";
//		String point ="??????";
		try {
			List<ZxjEip2Chbssjc> zxjEip2ChbssjcList = new ArrayList<ZxjEip2Chbssjc>();
			ZxjEip2Chbssjc zxjEip2Chbssjc = new ZxjEip2Chbssjc();
			ArrayList<ZxjEip2CHbzsTem> temperatures = new ArrayList<ZxjEip2CHbzsTem>(); 
			
			PreparedStatement pst = null;
			ResultSet rs = null;	

			String sql = "";	
			if (smt_bkbh!=null && !smt_bkbh.equals("") && point!=null && !point.equals("")){
				sql = "select * from zxj_eip_2chbzs_time where smt_bkbh ='" +smt_bkbh+"' And point='"+point+"'";	
			}else if (smt_bkbh!=null && !smt_bkbh.equals("")){
				sql = "select * from zxj_eip_2chbzs_time where smt_bkbh ='" +smt_bkbh+"'";	
			}else if (point!=null && !point.equals("")){
				sql = "select * from zxj_eip_2chbzs_time where point='"+point+"'";	
			}else{
				sql = "select * from zxj_eip_2chbzs_time";	
			}
			
			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();

			while (rs.next()) {
				ZxjEip2CHbzsTime boardCard = new ZxjEip2CHbzsTime(smt_bkbh,rs.getString("supplier"),rs.getString("factory"),
						rs.getString("deviceno"),rs.getString("point"),rs.getString("smt_yrq"),rs.getString("smt_hwq"),rs.getString("smt_hjq"),
						rs.getString("smt_lqq"),rs.getString("smt_sd"),rs.getString("smt_jcjl"),rs.getString("remark1"),rs.getString("remark2"),
						rs.getString("remark3"),rs.getString("remark4"),rs.getString("remark5"));
				
				zxjEip2Chbssjc.setBoardCard(boardCard);
				
//				String sql1 = "select \"smt_bkbh\",\"supplier\",\"factory\",\"device_no\",\"point\",\"zone\",\"temperature\",\"collect_time\",\"remark1\",\"remark2\",\"remark3\",\"remark4\",\"remark5\" from zxj_eip_2chbzs_tem where smt_bkbh='"+smt_bkbh +"' and point= '"+point+"'";
				
				String sql1 ="select * from zxj_eip_2chbzs_tem where smt_bkbh='"+smt_bkbh +"' and point= '"+point+"'";
				PreparedStatement pst1 = null;
				ResultSet rs1 = null;
				pst1 = db.getConnetion().prepareStatement(sql1);
				rs1 = pst1.executeQuery();
				
				while (rs1.next()) {
					ZxjEip2CHbzsTem zxjEip2CHbzsTem = new ZxjEip2CHbzsTem(rs.getString("smt_bkbh"),
							rs1.getString("supplier"),rs1.getString("factory"),//"","","","","",
							rs1.getString("device_no"),
							rs1.getString("point"),
							rs1.getString("zone"),
							rs1.getString("temperature"),
							rs1.getString("collect_time"),
							"","","","","");
					temperatures.add(zxjEip2CHbzsTem);
				}
				zxjEip2Chbssjc.setTemperatures(temperatures);
				
				rs1.close();
				pst1.close();
			}
			
			rs.close();
			pst.close();
			db.closeCn();
			
//			zxjEip2ChbssjcList.add(zxjEip2Chbssjc);
//			System.out.println(JSON.toJSONString(zxjEip2ChbssjcList));
			return zxjEip2Chbssjc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}	

	
	@GET
	@Path("/Chbssjc")
	@Produces(MediaType.APPLICATION_JSON)	
	public ZxjEip2ChbssjcArr getZxjEipChbjcss(@QueryParam("startTime") String startTime, @QueryParam("endTime") String endTime) {
//	public List<ZxjEip2Chbssjc> getZxjEipChbjcss(@RequestBody String startTime1, @RequestBody String endTime1) {		
		System.out.println("******************Chbssjc*******************"+startTime+"      "+endTime);
//		String startTime="2019-08-13 09:20:00";
//		String endTime ="2019-08-13 13:25:00";
		try {
			
//			List<ZxjEip2Chbssjc> zxjEip2ChbssjcList = new ArrayList<ZxjEip2Chbssjc>();
			ZxjEip2ChbssjcDate zxjEip2Chbssjc = new ZxjEip2ChbssjcDate();
			ArrayList<ZxjEip2CHbzsTime> boardCardList = new ArrayList<ZxjEip2CHbzsTime>();
			ArrayList<ZxjEip2CHbzsTem> temperaturesList = new ArrayList<ZxjEip2CHbzsTem>();
			
			PreparedStatement pst = null;
			ResultSet rs = null;	

			String sql = "select * from zxj_eip_2chbzs_time where smt_yrq Between to_timestamp('"+startTime+"','yyyy-mm-dd hh24:mi:ss') And to_timestamp('"+
					endTime+"','yyyy-mm-dd hh24:mi:ss')";
			
			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();

			while (rs.next()) {
				String smt_bkbh = rs.getString("smt_bkbh");
				ZxjEip2CHbzsTime boardCard = new ZxjEip2CHbzsTime(smt_bkbh,rs.getString("supplier"),rs.getString("factory"),
						rs.getString("deviceno"),rs.getString("point"),rs.getString("smt_yrq"),rs.getString("smt_hwq"),rs.getString("smt_hjq"),
						rs.getString("smt_lqq"),rs.getString("smt_sd"),rs.getString("smt_jcjl"),rs.getString("remark1"),rs.getString("remark2"),
						rs.getString("remark3"),rs.getString("remark4"),rs.getString("remark5"));
				
				boardCardList.add(boardCard);
			}
			rs.close();
			pst.close();
			System.out.println(JSON.toJSONString(boardCardList));
			
			PreparedStatement pst1 = null;
			ResultSet rs1 = null;
//			String sql1 = "select * from zxj_eip_2chbzs_tem where collect_time Between to_timestamp('"+startTime+"','yyyy-mm-dd hh24:mi:ss') And to_timestamp('"+
//				 endTime+"','yyyy-mm-dd hh24:mi:ss')";
			String sql1 = "select * from zxj_eip_devicetemperature where collect_time Between to_timestamp('"+startTime+"','yyyy-mm-dd hh24:mi:ss') And to_timestamp('"+
					 endTime+"','yyyy-mm-dd hh24:mi:ss')";
			
			pst1 = db.getConnetion().prepareStatement(sql1);
			rs1 = pst1.executeQuery();
			while (rs1.next()) {
				ZxjEip2CHbzsTem zxjEip2CHbzsTem = new ZxjEip2CHbzsTem("",//rs1.getString("smt_bkbh"),
						rs1.getString("supplier"),rs1.getString("factory"),//"","","","","",
						rs1.getString("device_no"),
						rs1.getString("point"),
						rs1.getString("zone"),
						rs1.getString("temperature"),
						rs1.getString("collect_time"),
						"","","","","");
				temperaturesList.add(zxjEip2CHbzsTem);
			}
			System.out.println(JSON.toJSONString(temperaturesList));
			rs1.close();
			pst1.close();
			db.closeCn();

			zxjEip2Chbssjc.setTimeList(boardCardList);
			zxjEip2Chbssjc.setTempList(temperaturesList);
			
			ZxjEip2ChbssjcArr ss = new ZxjEip2ChbssjcArr();
			ss.setSmt_hlh(zxjEip2Chbssjc);
			
//			zxjEip2ChbssjcList.add(zxjEip2Chbssjc);		
			System.out.println(JSON.toJSONString(ss));

			return ss;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	@GET
	@Path("/Cpzsn")
	@Produces(MediaType.APPLICATION_JSON)
	public List<ZxjEip2CPzsnInfo> getZxjEipCpzanInfo(@QueryParam("pgproductionorderno") String pg_productionOrderNo) {
		System.out.println("************Cpzsn******************   "+pg_productionOrderNo);
		try {
//			pg_productionOrderNo ="000301522958";
			ArrayList<ZxjEip2CPzsnInfo> zxjEipCpzsninfoList = new ArrayList<ZxjEip2CPzsnInfo>(); 
			
			PreparedStatement pst = null;
			ResultSet rs = null;			
			String sql = "";	
			if (pg_productionOrderNo!=null && !pg_productionOrderNo.equals("")){
				sql = "select * from zxj_eip_2cpzsn_info where pg_productionOrderno='"+pg_productionOrderNo+"'";	
			}else{
				return null;
			}

			pst = db.getConnetion().prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getString("pgbh"));
				ZxjEip2CPzsnInfo zxjEip2CPzsnInfo = new ZxjEip2CPzsnInfo(rs.getString("pg_productionorderno"),rs.getString("pgbh"),rs.getString("serialno"),
						rs.getString("zz_productionorderno"),rs.getString("zzbh"),"","","","","");
				zxjEipCpzsninfoList.add(zxjEip2CPzsnInfo);
				
			}
			rs.close();
			pst.close();
			db.closeCn();
			return zxjEipCpzsninfoList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}	
	
}
